﻿using System;
using System.IO;
using System.Text;
using Senparc.Weixin.MP.Context;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.MessageHandlers;
using Senparc.Weixin.MP.Helpers;
using HNAC.Weixin.CommonService.Cache;
using System.Data;
using HN.DBUtility;
using Common;
using HNAC.Weixin.CommonService.Model;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web;
using System.Collections.Generic;

namespace HNAC.Weixin.CommonService.CustomMessageHandler
{
    /// <summary>
    /// 自定义MessageHandler
    /// 把MessageHandler作为基类，重写对应请求的处理方法
    /// </summary>
    public partial class CustomMessageHandler : MessageHandler<MessageContext>
    {
        public CustomMessageHandler(Stream inputStream)
            : base(inputStream)
        {
            //这里设置仅用于测试，实际开发可以在外部更全局的地方设置，
            //比如MessageHandler<MessageContext>.GlobalWeixinContext.ExpireMinutes = 3。
            WeixinContext.ExpireMinutes = 3;
        }

        public override void OnExecuting()
        {
            //测试MessageContext.StorageData
            if (CurrentMessageContext.StorageData == null)
            {
                CurrentMessageContext.StorageData = 0;
            }
            base.OnExecuting();
        }

        public override void OnExecuted()
        {
            base.OnExecuted();
            CurrentMessageContext.StorageData = ((int)CurrentMessageContext.StorageData) + 1;
        }

        /// <summary>
        /// 处理文字请求
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
        {
            #region
            //TODO:这里的逻辑可以交给Service处理具体信息，参考OnLocationRequest方法或/Service/LocationSercice.cs

            //方法一（v0.1），此方法调用太过繁琐，已过时（但仍是所有方法的核心基础），建议使用方法二到四
            //var responseMessage =
            //    ResponseMessageBase.CreateFromRequestMessage(RequestMessage, ResponseMsgType.Text) as
            //    ResponseMessageText;

            //方法二（v0.4）
            //var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(RequestMessage);

            //方法三（v0.4），扩展方法，需要using Senparc.Weixin.MP.Helpers;
            //var responseMessage = RequestMessage.CreateResponseMessage<ResponseMessageText>();

            //方法四（v0.6+），仅适合在HandlerMessage内部使用，本质上是对方法三的封装
            //注意：下面泛型ResponseMessageText即返回给客户端的类型，可以根据自己的需要填写ResponseMessageNews等不同类型。
            #endregion
            var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
            responseMessage.Articles = new System.Collections.Generic.List<Article>();
            UserCacheData cachedata = AppCommon.Instance.Caches[requestMessage.FromUserName];
            string sqltext = string.Empty;
            //当前用户信息
            bool isLogin = false;
            string hznetuser, currentstaionid, currentstationname;
            UserOperationLevel oplevel = UserOperationLevel.None;
            DataSet dsuser = null;

            #region 从数据库中对比，确定用户是否已经有绑定关系
            try
            {
                sqltext = "select * from user where name='" + requestMessage.FromUserName + "'";
                dsuser = SQLiteHelper.ExecuteDataset(sqltext, GlobalVar.ShareDBConnStr);
                if (dsuser != null)
                {
                    if (dsuser.Tables.Count > 0 && dsuser.Tables[0].Rows.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(dsuser.Tables[0].Rows[0]["hznetuser"].ToString()))
                        {
                            isLogin = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                isLogin = false;
            }
            #endregion

            string groupid = string.Empty;
            if (requestMessage.Content == "约束")
            {
                //responseMessage.Content = "<a href=\"http://weixin.senparc.com/FilterTest/\">点击这里</a>进行客户端约束测试（地址：http://weixin.senparc.com/FilterTest/）。";
            }
            else
            {
                Article arc = new Article();
                try
                {
                    #region
                    //var result = new StringBuilder();
                    //result.AppendFormat("您刚才发送了文字信息：{0}\r\n\r\n", requestMessage.Content);

                    //if (CurrentMessageContext.RequestMessages.Count > 1)
                    //{
                    //    result.AppendFormat("您刚才还发送了如下消息（{0}）：\r\n", CurrentMessageContext.StorageData);
                    //    for (int i = CurrentMessageContext.RequestMessages.Count - 2; i >= 0; i--)
                    //    {
                    //        var historyMessage = CurrentMessageContext.RequestMessages[i];
                    //        result.AppendFormat("{0} 【{1}】{2}\r\n",
                    //                            historyMessage.CreateTime.ToShortTimeString(),
                    //                            historyMessage.MsgType.ToString(),
                    //                            (historyMessage is RequestMessageText)
                    //                                ? (historyMessage as RequestMessageText).Content
                    //                                : "[非文字类型]"
                    //            );
                    //    }
                    //    result.AppendLine("\r\n");
                    //}

                    //result.AppendFormat("如果您在{0}分钟内连续发送消息，记录将被自动保留。过期后记录将会自动清除。\r\n", WeixinContext.ExpireMinutes);
                    //result.AppendLine("\r\n");
                    //result.AppendLine("您还可以发送【位置】【图片】【语音】等类型的信息，查看不同格式的回复。\r\nSDK官方地址：http://weixin.senparc.com");

                    //responseMessage.Content = result.ToString();
                    #endregion
                    //if (cachedata != null && cachedata.IsLogin) //用户已登录
                    #region 已登录的处理流程
                    if (isLogin) //用户已登录
                    {

                        List<Article> arcs = new List<Article>();
                        arcs.Add(arc);
                        switch (cachedata.MenuLevel)
                        {
                            #region 其他情况
                            #region
                            //case UserOperationLevel.None:
                            //case UserOperationLevel.Login:
                            //default:
                            //    groupid = cachedata.GroupId;
                            //    if (string.IsNullOrEmpty(groupid))
                            //    {
                            //        groupid = UserLogin.GetGroupId(requestMessage.FromUserName);
                            //    }
                            //    arc.Title = "站点管理";
                            //    arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName, groupid, ref cachedata);
                            //    arc.PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.b9f6bef6047d1790be0ecb1b33613c41f01301bd.0a96bf.jpg?t=c4863a469cc6b4d229669f875fc616b8&d=20130712";
                            //    cachedata.IsLogin = true;
                            //    cachedata.GroupId = groupid;
                            //    cachedata.MenuLevel = UserOperationLevel.StationSelect;
                            //    break;
                            #endregion
                            case UserOperationLevel.None:
                            case UserOperationLevel.Login:
                            default:
                                arc.Title = "站点管理";
                                arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName);
                                arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                cachedata.MenuLevel = UserOperationLevel.StationSelect;
                                break;
                            #endregion

                            #region 处理站点选择菜单
                            case UserOperationLevel.StationSelect:
                                try
                                {
                                    int stationid = Convert.ToInt32(requestMessage.Content.Trim());
                                    if (stationid == 0)
                                    {//如果用户回复0，则退出登录
                                        UserLogin.LoginOut(requestMessage.FromUserName);
                                        arc.Title = "注销成功";
                                        arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                                        arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                        responseMessage.Articles.Add(arc);
                                    }
                                    else
                                    {
                                        #region
                                        //DataRow[] drs = cachedata.Stations.Tables[0].Select("stationid = " + stationid + "");
                                        //if (drs == null || drs.Length == 0)
                                        //{
                                        //    arc.Title = "站点编号输入错误";
                                        //    arc.Description = "0.退出登录\n" + cachedata.StationString + "\n";
                                        //}
                                        //else
                                        //{
                                        //    string stationname = drs[0]["stationname"].ToString();
                                        //    arc.Title = "欢迎进入" + stationname + "站点!";
                                        //    responseMessage.Articles.Add(new Article()
                                        //    {
                                        //        Description = "1.查看报警数据\n",
                                        //        PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.aa892a17fdf0d7cef0d336f7dadfb5854e221e1e.69b7ee.jpg?t=3d00f942b1b51305726d4a15475603ef&d=20130715",
                                        //    });
                                        //    responseMessage.Articles.Add(new Article()
                                        //    {
                                        //        Description = "2.查看实时数据\n",
                                        //        PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.aa892a17fdf0d7cef0d336f7dadfb5854e221e1e.69b7ee.jpg?t=3d00f942b1b51305726d4a15475603ef&d=20130715",
                                        //    });
                                        //    responseMessage.Articles.Add(new Article()
                                        //    {
                                        //        Description = "3.查看历史遥测遥信\n",
                                        //        PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.aa892a17fdf0d7cef0d336f7dadfb5854e221e1e.69b7ee.jpg?t=3d00f942b1b51305726d4a15475603ef&d=20130715",
                                        //    });
                                        //    responseMessage.Articles.Add(new Article()
                                        //    {
                                        //        Description = "4.发送遥控指令\n",
                                        //        PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.aa892a17fdf0d7cef0d336f7dadfb5854e221e1e.69b7ee.jpg?t=3d00f942b1b51305726d4a15475603ef&d=20130715",
                                        //    });
                                        //    arc.Description = "请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                        //    cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                        //    cachedata.StationName = stationname;
                                        //}
                                        #endregion
                                        #region
                                        if (StationsBussiness.IsExistStation(requestMessage.FromUserName, stationid))
                                        {
                                            string stationname = StationsBussiness.GetStationName(requestMessage.FromUserName, stationid);
                                            arc.Title = "欢迎进入" + stationname + "站点!\n请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                            //cachedata.StationName = stationname;
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            responseMessage.Articles.Add(arc);
                                            responseMessage.Articles.Add(new Article()
                                                {
                                                    Title = "1.查看报警数据",
                                                    PicUrl = AppCommon.ALERTIMAGE,
                                                });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "2.查看实时数据",
                                                PicUrl = AppCommon.REALDATA,
                                            });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "3.查看历史曲线",
                                                PicUrl = AppCommon.HISTORYCURVE,
                                            });
                                            
                                            cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                        }
                                        else
                                        {
                                            arc.Title = "站点编号输入错误";
                                            arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                            arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName);
                                            cachedata.MenuLevel = UserOperationLevel.StationSelect;
                                            responseMessage.Articles.Add(arc);
                                        }
                                        #endregion
                                    }
                                }
                                catch
                                {
                                    arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                    arc.Title = "对不起,出错了！";
                                    arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName);
                                    responseMessage.Articles.Add(arc);
                                    cachedata.MenuLevel = UserOperationLevel.StationSelect;
                                    cachedata.StationNo = "";
                                }
                                break;
                            #endregion

                            #region 处理实时遥测遥信数据查询菜单
                            case UserOperationLevel.RealYcYxView:
                                try
                                {
                                    int selectycindex = Convert.ToInt32(requestMessage.Content);
                                    switch (selectycindex)
                                    {
                                        case 0: //退出登录
                                            UserLogin.LoginOut(requestMessage.FromUserName);
                                            arc.Title = "注销成功";
                                            arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            responseMessage.Articles.Add(arc);
                                            break;
                                        case 99: //返回上级菜单
                                            string stationname = cachedata.StationIdandName[cachedata.StationNo];
                                            arc.Title = "欢迎进入" + stationname + "站点!\n请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            responseMessage.Articles.Add(arc);
                                            responseMessage.Articles.Add(new Article()
                                                {
                                                    Title = "1.查看报警数据",
                                                    PicUrl = AppCommon.ALERTIMAGE,
                                                });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "2.查看实时数据",
                                                PicUrl = AppCommon.REALDATA,
                                            });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "3.查看历史曲线",
                                                PicUrl = AppCommon.HISTORYCURVE,
                                            });
                                            cachedata.MenuLevel = UserOperationLevel.SelectViewData;

                                            break;
                                        default:
                                            if (StationsBussiness.IsExistRealDataFile(requestMessage.FromUserName, selectycindex))
                                            {
                                                ArrayList ar = StationsBussiness.GetRealDataFileCache(requestMessage.FromUserName, selectycindex);
                                                arc.Title = StationsBussiness.GetRealDataCaption(requestMessage.FromUserName, selectycindex);
                                                if (ar.Count != 0)
                                                {
                                                    //arc.PicUrl = StationsBussiness.GetRealDataFileUrl(requestMessage.FromUserName, selectycindex);
                                                    arc.PicUrl = AppCommon.REALDATA;
                                                    WebCache.SetCache(ar[0].ToString(), ar[1], DateTime.Now.AddMinutes(30), TimeSpan.Zero);
                                                    arc.Url = AppCommon.REALDATAURL + "?id=" + ar[0].ToString();
                                                }
                                                else
                                                {
                                                    arc.PicUrl = AppCommon.REALDATA;
                                                    arc.Url = "";
                                                }
                                                arc.Description = "请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                            }
                                            else
                                            {
                                                arc.Title = "你回复的编号有误";
                                                arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                                arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                                cachedata.MenuLevel = UserOperationLevel.RealYcYxView;
                                            }
                                            responseMessage.Articles.Add(arc);
                                            break;
                                    }
                                }
                                catch(Exception ex)
                                {
                                    AppCommon.Instance.LoggerHandler.Log(ex.Message);
                                    arc.Title = "对不起,出错了";
                                    arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                    arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                    responseMessage.Articles.Add(arc);
                                    cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                }
                                break;
                            #endregion

                            #region 历史遥测遥信数据查询菜单
                            case UserOperationLevel.HisYcYxView:
                                try
                                {
                                    int selectycindex = Convert.ToInt32(requestMessage.Content);
                                    switch (selectycindex)
                                    {
                                        case 0: //退出登录
                                            UserLogin.LoginOut(requestMessage.FromUserName);
                                            arc.Title = "注销成功";
                                            arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            responseMessage.Articles.Add(arc);
                                            break;
                                        case 99: //返回上级菜单
                                            string stationname = cachedata.StationIdandName[cachedata.StationNo];
                                            arc.Title = "欢迎进入" + stationname + "站点!\n请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            responseMessage.Articles.Add(arc);
                                            responseMessage.Articles.Add(new Article()
                                                {
                                                    Title = "1.查看报警数据",
                                                    PicUrl = AppCommon.ALERTIMAGE,
                                                });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "2.查看实时数据",
                                                PicUrl = AppCommon.REALDATA,
                                            });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "3.查看历史曲线",
                                                PicUrl = AppCommon.HISTORYCURVE,
                                            });
                                            cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                            break;
                                        default:
                                            arc.Title = "你回复的编号有误";
                                            arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                            cachedata.MenuLevel = UserOperationLevel.HisYcYxView;
                                            responseMessage.Articles.Add(arc);
                                            break;
                                    }
                                }
                                catch
                                {
                                    cachedata.MenuLevel = UserOperationLevel.HisYcYxView;
                                    arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                    arc.Title = "对不起,出错了";
                                    arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                    responseMessage.Articles.Add(arc);
                                }
                                break;
                            #endregion

                            #region 处理选择数据菜单
                            case UserOperationLevel.SelectViewData:
                                try
                                {
                                    int selectycindex = Convert.ToInt32(requestMessage.Content);
                                    switch (selectycindex)
                                    {
                                        case 0: //退出登录
                                            UserLogin.LoginOut(requestMessage.FromUserName);
                                            arc.Title = "注销成功";
                                            arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            break;
                                        case 1:
                                            arc.Title = "最新报警,请选择你要查看的数据，回复对应的数字序号。\n0.退出登录\n99.返回上级菜单";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            arc.Description = "请选择你要查看的数据，回复对应的数字序号。\n0.退出登录\n99.返回上级菜单";
                                            arcs.AddRange(StationsBussiness.GetAlertDataFilesPic(requestMessage.FromUserName, cachedata.StationIdandName[cachedata.StationNo]));
                                            cachedata.MenuLevel = UserOperationLevel.AlertDataView;
                                            //arc.Description = "现在进入报警页面." + StationsBussiness.GetAlertData(ref cachedata) + "\n回复99返回上一级菜单\n回复0退出登录\n";
                                            break;
                                        case 2:
                                            arc.Title = "实时遥测,请选择你要查看的数据，回复对应的数字序号。\n0.退出登录\n99.返回上级菜单";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            arc.Description = "请选择你要查看的数据，回复对应的数字序号。\n0.退出登录\n99.返回上级菜单";
                                            //arc.Description = StationsBussiness.GetRealDataFiles(requestMessage.FromUserName, cachedata.StationIdandName[cachedata.StationNo]);
                                            arcs.AddRange(StationsBussiness.GetRealDataFilesPic(requestMessage.FromUserName, cachedata.StationIdandName[cachedata.StationNo]));
                                            cachedata.MenuLevel = UserOperationLevel.RealYcYxView;
                                            break;
                                        case 3:
                                            arc.Title = "历史数据曲线,请选择你要查看的数据，回复对应的数字序号。\n0.退出登录\n99.返回上级菜单";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            arc.Description = "请选择你要查看的数据，回复对应的数字序号。\n0.退出登录\n99.返回上级菜单";
                                            arcs.AddRange(StationsBussiness.GetHistoryDataFilesPic(requestMessage.FromUserName, cachedata.StationIdandName[cachedata.StationNo]));
                                            cachedata.MenuLevel = UserOperationLevel.HisYcYxView;
                                            break;
                                        case 99:
                                            arc.Title = "站点管理";
                                            arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName) + "\n";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            cachedata.IsLogin = true;
                                            cachedata.MenuLevel = UserOperationLevel.StationSelect;
                                            break;
                                        default:
                                            arc.Title = "你回复的编号有误";
                                            arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                            arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                            cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                            break;
                                    }
                                }
                                catch
                                {
                                    string stationname = StationsBussiness.GetStationName(requestMessage.FromUserName, cachedata.StationNo);
                                    arc.Title = "欢迎进入" + stationname + "站点!\n请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                    //cachedata.StationName = stationname;
                                    arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                    responseMessage.Articles.Add(arc);
                                    responseMessage.Articles.Add(new Article()
                                    {
                                        Title = "1.查看报警数据",
                                        PicUrl = AppCommon.ALERTIMAGE,
                                    });
                                    responseMessage.Articles.Add(new Article()
                                    {
                                        Title = "2.查看实时数据",
                                        PicUrl = AppCommon.REALDATA,
                                    });
                                    responseMessage.Articles.Add(new Article()
                                    {
                                        Title = "3.查看历史曲线",
                                        PicUrl = AppCommon.HISTORYCURVE,
                                    });
                                    cachedata.MenuLevel = UserOperationLevel.SelectViewData;

                                    arc.Title = "对不起,出错了";
                                    arc.PicUrl = AppCommon.ERRORIMAGEURL;
                                    arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                    cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                }
                                finally
                                {
                                    responseMessage.Articles.AddRange(arcs);
                                }
                                break;
                            #endregion

                            #region 处理报警菜单
                            case UserOperationLevel.AlertDataView:
                                try
                                {
                                    switch (Convert.ToInt32(requestMessage.Content))
                                    {
                                        case 0: //退出登录
                                            UserLogin.LoginOut(ref cachedata);
                                            arc.Title = "注销成功";
                                            arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                                            arc.PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.b9f6bef6047d1790be0ecb1b33613c41f01301bd.0a96bf.jpg?t=c4863a469cc6b4d229669f875fc616b8&d=20130712";
                                            responseMessage.Articles.Add(arc);
                                            cachedata.IsLogin = false;
                                            break;
                                        case 99: //返回上级菜单
                                            string stationname = cachedata.StationIdandName[cachedata.StationNo];
                                            arc.Title = "欢迎进入" + stationname + "站点!\n请选择你要查看的数据(回复序号)\n0.退出登录\n99.返回上一级目录\n";
                                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                                            responseMessage.Articles.Add(arc);
                                            responseMessage.Articles.Add(new Article()
                                                {
                                                    Title = "1.查看报警数据",
                                                    PicUrl = AppCommon.ALERTIMAGE,
                                                });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "2.查看实时数据",
                                                PicUrl = AppCommon.REALDATA,
                                            });
                                            responseMessage.Articles.Add(new Article()
                                            {
                                                Title = "3.查看历史曲线",
                                                PicUrl = AppCommon.HISTORYCURVE,
                                            });
                                            cachedata.MenuLevel = UserOperationLevel.SelectViewData;
                                            break;
                                        default:
                                            break;
                                    }
                                }
                                catch
                                {
                                    cachedata.MenuLevel = UserOperationLevel.AlertDataView;
                                    arc.Title = "对不起,出错了";
                                    arc.Description = "请选择你要查看的数据(回复序号)\n1.查看最新警告数据\n2.查看实时数据\n3.查看历史遥测遥信\n99.返回上一级目录\n";
                                    responseMessage.Articles.Add(arc);
                                }
                                break;
                            #endregion

                        }
                    }
                    #endregion

                    #region 验证登录过程
                    else
                    {//用户未登录

                        if (UserLogin.Login(requestMessage.FromUserName, requestMessage.Content))
                        {
                            hznetuser = requestMessage.Content.Split(',')[0];

                            arc.Title = "站点管理";
                            arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName);
                            arc.PicUrl = AppCommon.LOGOIMAGEURL;
                            responseMessage.Articles.Add(arc);
                            cachedata = AppCommon.Instance.Caches[requestMessage.FromUserName];
                            cachedata.IsLogin = true;
                            cachedata.MenuLevel = UserOperationLevel.StationSelect;
                        }
                        else
                        {
                            arc.Title = "登录失败了";
                            arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                            arc.PicUrl = AppCommon.ERRORIMAGEURL;
                            cachedata = AppCommon.Instance.Caches[requestMessage.FromUserName];
                            responseMessage.Articles.Add(arc);
                            cachedata.IsLogin = false;
                        }
                        #region
                        //groupid = UserLogin.Login(requestMessage.FromUserName,requestMessage.Content, ref cachedata);
                        //Article arc = new Article();
                        //if (!string.IsNullOrEmpty(groupid))//登录通过
                        //{
                        //    arc.Title = "站点管理";
                        //    arc.Description = StationsBussiness.GetStations(requestMessage.FromUserName, groupid, ref cachedata);
                        //    arc.PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.b9f6bef6047d1790be0ecb1b33613c41f01301bd.0a96bf.jpg?t=c4863a469cc6b4d229669f875fc616b8&d=20130712";
                        //    responseMessage.Articles.Add(arc);
                        //    cachedata.IsLogin = true;
                        //    cachedata.MenuLevel = UserOperationLevel.StationSelect;   
                        //}
                        //else
                        //{//登录失败
                        //    arc.Title = "登录失败了";
                        //    arc.Description = "请先登录，登录格式为：用户名，密码。用户名和密码用逗号间隔开。";
                        //    arc.PicUrl = "http://t17-2.yunpan.360.cn/p/800-600.b9f6bef6047d1790be0ecb1b33613c41f01301bd.0a96bf.jpg?t=c4863a469cc6b4d229669f875fc616b8&d=20130712";
                        //    responseMessage.Articles.Add(arc);
                        //    cachedata.IsLogin = false;
                        //}
                        #endregion
                    }
                    #endregion
                }
                catch(Exception ex)
                {
                    AppCommon.Instance.LoggerHandler.Log(ex.Message);
                    arc.Title = "出错了，请联系管理员!";
                    arc.PicUrl = AppCommon.ERRORIMAGEURL;
                    responseMessage.Articles.Add(arc);
                }
                finally
                {
                    AppCommon.Instance.Caches[requestMessage.FromUserName] = cachedata;
                }
            }
            return responseMessage;
            //ServerCache.SetCache(requestMessage.FromUserName, cachedata, DateTime.Now.AddDays(1), TimeSpan.Zero);
        }

        /// <summary>
        /// 处理位置请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
        {
            var locationService = new LocationService();
            var responseMessage = locationService.GetResponseMessage(requestMessage as RequestMessageLocation);
            return responseMessage;
        }

        /// <summary>
        /// 处理图片请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
        {
            //var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageNews>(RequestMessage);
            //responseMessage.Articles.Add(new Article()
            //{
            //    Title = "您刚才发送了图片信息",
            //    Description = "您发送的图片将会显示在边上",
            //    PicUrl = requestMessage.PicUrl,
            //    Url = "http://weixin.senparc.com"
            //});
            //responseMessage.Articles.Add(new Article()
            //{
            //    Title = "第二条",
            //    Description = "第二条带连接的内容",
            //    PicUrl = requestMessage.PicUrl,
            //    Url = "http://weixin.senparc.com"
            //});
            var image = new ImageService();
            var responseMessage = image.GetResponseMessage(requestMessage as RequestMessageImage);
            return responseMessage;
        }

        /// <summary>
        /// 处理语音请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
        {
            var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageMusic>(requestMessage);
            responseMessage.Music.MusicUrl = "http://weixin.senparc.com/Content/music1.mp3";
            return responseMessage;
        }

        /// <summary>
        /// 处理链接消息请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
        {
            var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage);
            responseMessage.Content = string.Format(@"您发送了一条连接信息：
Title：{0}
Description:{1}
Url:{2}", requestMessage.Title, requestMessage.Description, requestMessage.Url);
            return responseMessage;
        }

        /// <summary>
        /// 处理事件请求（这个方法一般不用重写，这里仅作为示例出现。除非需要在判断具体Event类型以外对Event信息进行统一操作
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEventRequest(RequestMessageEventBase requestMessage)
        {
            var eventResponseMessage = base.OnEventRequest(requestMessage);//对于Event下属分类的重写方法，见：CustomerMessageHandler_Events.cs
            //TODO: 对Event信息进行统一操作
            return eventResponseMessage;
        }

    }
}
